package git.soulbgm.common.map;

import git.soulbgm.common.GroupValue;
import git.soulbgm.enumeration.GroupType;
import net.jodah.expiringmap.ExpirationListener;
import net.jodah.expiringmap.ExpirationPolicy;
import net.jodah.expiringmap.ExpiringMap;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * 程序入口
 *
 * @author SoulBGM
 * @date 2022/02/16
 */
public class GroupMap {

    private final static ExpiringMap<String, Map<String, Map<String, GroupValue[]>>> GROUP_CACHE_DATA_MAP;

    static {
        GROUP_CACHE_DATA_MAP = ExpiringMap.builder()
                // 变量过期 条目可以具有单独可变的过期时间和策略
                .variableExpiration()
                // map最大尺寸
                .maxSize(100)
                // 过期策略 ACCESSED 根据条目最后一次被访问的时间来过期 | CREATED 根据条目的创建时间来过期
                //.expirationPolicy(ExpirationPolicy.ACCESSED)
                // 过期时间
                //.expiration(20, TimeUnit.SECONDS)
                // 同步过期侦听器
                //.expirationListener((ExpirationListener<String, Map<String, Map<String, GroupValue[]>>>) (k, v) -> output(k, v))
                // 异步过期侦听器
                .asyncExpirationListener((ExpirationListener<String, Map<String, Map<String, GroupValue[]>>>) (k, v) -> output(k, v))
                .build();
    }

    public static void main(String[] args) throws InterruptedException {
        Map<String, GroupValue[]> map = new HashMap<>();
        GroupValue[] gv = new GroupValue[4];
        gv[0] = new GroupValue(GroupType.COUNT, 1);
        gv[1] = new GroupValue(GroupType.SUM, 20);
        gv[2] = new GroupValue(GroupType.MAX, 10);
        gv[3] = new GroupValue(GroupType.MIN, 10);
        map.put("123_100_1", gv);
        Map<String, Map<String, GroupValue[]>> map1 = new HashMap<>();
        map1.put("receivedDataMinute", map);
        GROUP_CACHE_DATA_MAP.put("m_2022-2-16-16-30", map1, ExpirationPolicy.ACCESSED, 20, TimeUnit.SECONDS);
        Thread.sleep(10 * 1000);
        gv[0].change(1);
        gv[1].change(22);
        gv[2].change(12);
        gv[3].change(8);
        Thread.sleep(9 * 1000);
        final Set<String> keySet = GROUP_CACHE_DATA_MAP.keySet();
        for (String k : keySet) {
            final Map<String, Map<String, GroupValue[]>> map2 = GROUP_CACHE_DATA_MAP.get(k);
            output(k, map2);
        }
        Thread.sleep(20 * 1000);
    }

    private static void output(String key, Map<String, Map<String, GroupValue[]>> map2) {
        System.out.printf("key1 : %s\n", key);
        final Set<String> keySet1 = map2.keySet();
        for (String k1 : keySet1) {
            System.out.printf("key2 : %s\n", k1);
            final Map<String, GroupValue[]> map3 = map2.get(k1);
            final Set<String> keySet2 = map3.keySet();
            for (String k2 : keySet2) {
                System.out.printf("key3 : %s\n", k2);
                final GroupValue[] values = map3.get(k2);
                for (GroupValue v : values) {
                    System.out.printf("%s : %s\n", v.getType(), v.getValue().longValue());
                }
            }
        }
    }

}
